package com.tarena.lbs.business.servcie.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.dao.mapper.BusinessMapper;
import com.tarena.lbs.business.dao.mapper.StoreMapper;
import com.tarena.lbs.business.pojo.param.BusinessParam;
import com.tarena.lbs.business.pojo.po.BusinessPO;
import com.tarena.lbs.business.pojo.query.BusinessQuery;
import com.tarena.lbs.business.pojo.vo.BusinessStoreVO;
import com.tarena.lbs.business.pojo.vo.BusinessVO;
import com.tarena.lbs.business.servcie.BusinessService;
import com.tarena.lbs.common.enums.PassportResultEnum;
import com.tarena.lbs.common.enums.Roles;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.common.utils.AuthenticationContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class BusinessServiceImpl implements BusinessService {
	
	@Autowired
	private BusinessMapper businessMapper;
	@Autowired
	private StoreMapper storeMapper;
	
	// 新增商家
	@Override
	public void save(BusinessParam param) throws BusinessException {
		log.debug(">>>新增商家-业务-入参：{}", param);
		// 获取登录用户信息
		Roles role = verifyIdentity();
		// 检查权限
		Asserts.isTrue(role != Roles.ADMIN, new BusinessException(PassportResultEnum.NOT_PERMISSION));
		// 检查商家名称
		Asserts.isTrue(businessMapper.queryByName(param.getBusinessName()) != null, new BusinessException(PassportResultEnum.BUSINESS_NAME_EXIST));
		// 保存
		BusinessPO po = new BusinessPO();
		BeanUtils.copyProperties(param, po);
		po.setEntryTime(new Date());
		int result = businessMapper.save(po);
		log.debug(">>>新增商家-业务-结果，result：{}", result);
		Asserts.isTrue(result <= 0, new BusinessException(PassportResultEnum.SAVE_FAILED));
		// TODO 获取保存后的商家id，调用附件服务绑定图片信息
	}
	
	// 查询商家列表
	@Override
	public PageResult<BusinessVO> pageList(BusinessQuery query) throws BusinessException {
		log.debug(">>>查询商家列表-业务-入参：{}", query);
		// 获取登录用户信息
		Roles role = verifyIdentity();
		// 如果是商家角色，获取商家ID
		if (role == Roles.SHOP) {
			UserPrinciple userPrinciple = AuthenticationContextUtils.get();
			query.setBusinessId(userPrinciple.getId());
		}
		
		// 分页结果对象
		PageResult<BusinessVO> pageResult = new PageResult<>(query);
		// 分页设置
		PageHelper.startPage(query.getPageNo(), query.getPageSize());
		// 查询
		List<BusinessPO> poList = businessMapper.selectList(query);
		// 封装分页信息
		PageInfo<BusinessPO> pageInfo = new PageInfo<>(poList);
		pageResult.setPageNo(query.getPageNo());
		pageResult.setPageSize(query.getPageSize());
		pageResult.setTotal(pageInfo.getTotal());
		
		// 初始化VO列表
		List<BusinessVO> vos = null;
		// 获取查询结果PO列表
		List<BusinessPO> pos = pageInfo.getList();
		
		// 判断PO列表是否为空，不为空则进行转换
		if (CollectionUtils.isNotEmpty(pos)) {
			vos = pos.stream().map(po -> {
				BusinessVO vo = new BusinessVO();
				BeanUtils.copyProperties(po, vo);
				return vo;
			}).collect(Collectors.toList());
		}
		// 设置VO列表到分页结果中
		pageResult.setObjects(vos);
		// 返回分页结果
		return pageResult;
		
	}
	
	// 查询商家详情
	@Override
	public BusinessStoreVO getDetail(Integer businessId) {
		log.debug(">>>查询商家详情-业务-入参：{}", businessId);
		BusinessStoreVO vo = businessMapper.getDetailById(businessId);
		return vo;
	}
	
	
	/**
	 * 验证用户身份
	 *
	 * @return 身份信息
	 * @throws BusinessException 业务异常未登录
	 */
	private Roles verifyIdentity() throws BusinessException {
		// 获取登录用户信息
		UserPrinciple userPrinciple = AuthenticationContextUtils.get();
		log.debug(">>>业务-登录用户信息：{}", userPrinciple);
		// 检查登录状态
		Asserts.isTrue(userPrinciple == null, new BusinessException(PassportResultEnum.NOT_LOGIN));
		// 获取角色
		return userPrinciple.getRole();
	}
	
}
